﻿namespace UnityEditor
{
    using System;
    using System.IO;
    using UnityEngine;

    [Serializable]
    internal class CreateAssetUtility
    {
        [SerializeField]
        private ProjectWindowUtil.EndAction m_EndAction;
        [SerializeField]
        private Texture2D m_Icon;
        [SerializeField]
        private int m_InstanceID;
        [SerializeField]
        private string m_Path = string.Empty;
        [SerializeField]
        private string m_ResourceFile;

        public void BeginNewAssetCreation(int instanceID, ProjectWindowUtil.EndAction newAssetEndAction, string pathName, Texture2D icon, string newAssetResourceFile)
        {
            if (!pathName.StartsWith("assets/", StringComparison.CurrentCultureIgnoreCase))
            {
                pathName = AssetDatabase.GetUniquePathNameAtSelectedPath(pathName);
            }
            else
            {
                pathName = AssetDatabase.GenerateUniqueAssetPath(pathName);
            }
            this.m_InstanceID = instanceID;
            this.m_Path = pathName;
            this.m_Icon = icon;
            this.m_EndAction = newAssetEndAction;
            this.m_ResourceFile = newAssetResourceFile;
            Selection.activeObject = EditorUtility.InstanceIDToObject(instanceID);
        }

        public void Clear()
        {
            this.m_EndAction = ProjectWindowUtil.EndAction.None;
            this.m_InstanceID = 0;
            this.m_Path = string.Empty;
            this.m_Icon = null;
            this.m_ResourceFile = string.Empty;
        }

        public void EndNewAssetCreation(string name)
        {
            string pathName = this.folder + "/" + name + this.extension;
            ProjectWindowUtil.EndAction endAction = this.m_EndAction;
            int instanceID = this.m_InstanceID;
            string resourceFile = this.m_ResourceFile;
            this.Clear();
            ProjectWindowUtil.EndNameEditAction(endAction, instanceID, pathName, resourceFile);
        }

        public bool IsCreatingNewAsset()
        {
            return !string.IsNullOrEmpty(this.m_Path);
        }

        public string extension
        {
            get
            {
                return Path.GetExtension(this.m_Path);
            }
        }

        public string folder
        {
            get
            {
                return Path.GetDirectoryName(this.m_Path);
            }
        }

        public Texture2D icon
        {
            get
            {
                return this.m_Icon;
            }
        }

        public int instanceID
        {
            get
            {
                return this.m_InstanceID;
            }
        }

        public string originalName
        {
            get
            {
                return Path.GetFileNameWithoutExtension(this.m_Path);
            }
        }
    }
}

